// pages/movieDetail/movieDetail.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    movieDetail: [],
    shoucangOrNot: 0,
    movieId: '',
    commentList: [],
    starScore: '',
    stars: [
      {
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      },
      {
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      },
      {
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      },
      {
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      },
      {
        bgImg: "/img/icon_bsta_no.png",
        bgfImg: "/img/icon_bstar1.png"
      }
    ],
    remark_num: 0.0 ,
    int: "",
    percent: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.setData({
      movieId: options.movieId
    })
    var collect = {
      userId: wx.getStorageSync('userInfo').id,
      movieId: options.movieId
    }
    wx.request({
      url: 'http://localhost:8081/api/movie/getMovieDetail/' + options.movieId,
      method: 'GET',
      success: function (res) {
        that.setData({
          movieDetail: res.data.result
        })
      }
    })
    wx.request({
      url: 'http://localhost:8081/api/collect/collectOrNot',
      method: 'POST',
      data: collect,
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.code == 10008) {
          that.setData({
            shoucangOrNot: res.data.result
          })
        } else {
          wx.showToast({
            title: '数据错误',
            icon: 'error'
          })
        }
      }
    }),
    wx.request({
      url: 'http://localhost:8081/api/comment/getComment',
      data: {
        movieId: options.movieId
      },
      method: 'GET',
      success:function(res){
        that.setData({
          commentList: res.data.result
        })
      }
    }),
    wx.request({
      url: 'http://localhost:8081/api/comment/getStartScore',
      method: 'GET',
      data: {
        movieId : options.movieId
      },
      success:function(res){
        that.setData({
          remark_num : res.data.result
        })
        var int = Math.floor(that.data.remark_num);
        var percent = (that.data.remark_num - int) * 100
        that.setData({
          int: int,
          percent: percent
        })
      }
    })
  },


  // 跳转
  toSelectCinemaAndDate() {
    var that = this
    wx.navigateTo({
      url: '../selectCinemaAndDate/selectCinemaAndDate?movieName=' + that.data.movieDetail.movieName
    })
    wx.setStorageSync('selectMovieId', that.data.movieDetail.id)
    wx.setStorageSync('selectMovieName', that.data.movieDetail.movieName)
  },

  // 收藏事件
  changeshoucang() {
    var that = this
    var collect = {
      userId: wx.getStorageSync('userInfo').id,
      movieId: that.data.movieId
    }
    wx.request({
      url: 'http://localhost:8081/api/collect/collectMovie',
      data: collect,
      method: "POST",
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.code == 10008) {
          wx.request({
            url: 'http://localhost:8081/api/collect/collectOrNot',
            method: 'POST',
            data: collect,
            header: {
              'token': wx.getStorageSync('token')
            },
            success: function (res) {
              if (res.data.code == 10008) {
                that.setData({
                  shoucangOrNot: res.data.result
                })
              } else {
                wx.showToast({
                  title: '数据错误',
                  icon: 'error'
                })
              }
            }
          })
        }
      }
    })
    // if (that.data.shoucangOrNot == 0) {
    //   that.setData({
    //     shoucangOrNot: 1
    //   })
    // } else {
    //   that.setData({
    //     shoucangOrNot: 0
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})